package app.beit.finportal.controller;

import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import app.beit.finportal.bean.MSConfigBean;
import app.beit.finportal.bean.MenuBean;
import app.beit.finportal.service.MSConfigService;
import app.beit.finportal.service.MenuService;
import app.beit.finportal.utils.constr.MsgConstr;
import app.beit.finportal.utils.constr.SessionConstr;

public abstract class BaseController {
	
	@Autowired
	private MenuService menuService;
	@Autowired
	private MSConfigService msConfigService;
	
	protected void startProcess(HttpServletRequest request) throws Exception {
		final List<MenuBean> menus = this.menuService.getAllMenu();
		final List<MenuBean> activeMenus = this.menuService.getAllActiveMenu();
		final MSConfigBean msconfig = this.msConfigService.getIsInternet();
		
		request.setAttribute("menus", menus);
		request.setAttribute("activemenus", activeMenus);
		request.setAttribute("masterConfig", msconfig);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected FintrackResponse responseError(final String code, final String msg) {
		final FintrackResponse response = new FintrackResponse();
		response.setCode(code);
		response.setMsg(msg);
		response.setSuccess(false);
		response.setDatas(null);
		return response;
	}

	@SuppressWarnings({ "rawtypes"})
	protected FintrackResponse responseSuccess() {
		final FintrackResponse response = new FintrackResponse();
		response.setCode(MsgConstr.SUCCESS_CODE);
		response.setMsg(MsgConstr.SUCCESS_MSG);
		response.setSuccess(true);
		
		return response;
	}

	@SuppressWarnings({ "rawtypes", "unchecked"})
	protected FintrackResponse responseSuccessNullData() {
		final FintrackResponse response = new FintrackResponse();
		response.setCode(MsgConstr.SUCCESS_NULL_DATA_CODE);
		response.setMsg(MsgConstr.SUCCESS_NULL_DATA_MSG);
		response.setSuccess(true);
		response.setDatas(null);
		
		return response;
	}
	
	@SuppressWarnings("rawtypes")
	protected void removeAllSession(final HttpServletRequest request) {
		final Enumeration names = request.getSession().getAttributeNames();
		while (names.hasMoreElements()) {
			final String attrname = (String) names.nextElement();
			request.getSession().removeAttribute(attrname);
		}
	}
	
	@SuppressWarnings("rawtypes")
	protected void removeSessionNotUser(final HttpServletRequest request) {
		final Enumeration names = request.getSession().getAttributeNames();
		while (names.hasMoreElements()) {
			final String attrname = (String) names.nextElement();
			if(!attrname.equalsIgnoreCase(SessionConstr.USER)){
				request.getSession().removeAttribute(attrname);
			}
		}
	}
	
}
